<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta content="telephone=no" name="format-detection" />
    <!-- 留作将来统一添加 -->
    <TEMPLATE>${TEMPLATE}</TEMPLATE>
    <title>模板html</title>
    <link rel="stylesheet" type="text/css" href="../../css/api.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/aui.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/index.css">
    <link rel="stylesheet" type="text/css" href="../../css/data-flex.min.css">
    <style type="text/css">
        body {
            height: 100vh;
            background-color: #002F4A;
        }
        .video-box {
            position: fixed;
            left: 0;
            top: 0;
            right: 0;
            bottom: 60px;
        }
        .video-box img {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 50px;
            height: 50px;
        }
        .footer {
            position: fixed;
            right: 0;
            bottom: 30px;
            left: 0;
            padding: 0 15px;
            height: 30px;
        }
        .footer img {
            width: 30px;
            height: 30px;
        }
        .footer .time {
            color: #fff;
            text-align: right;
            font-size: 12px;
        }
        .progress-box {
            position: relative;
            margin: 0 15px;
            height:4px;
            background: rgba(0,0,0,0.48);
            border-radius: 2px;
            overflow: initial;
        }
        .progress {
            background-color: #fff;
            border-radius: 2px;
            width: 0%;
            height: 4px;
        }
        .point {
            position: absolute;
            top: 2px;
            left: 0%;
            width: 12px;
            height: 12px;
            border-radius: 50%;
            border: 3px solid rgba(255,255,255,0.35);
            box-sizing: content-box;
            transform: translate(-50%, -50%);
        }
        .point::before {
            content: ' ';
            position: absolute;
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background-color: #fff;
        }
    </style>
</head>
<body>
    <div onclick="handlePlay()" class="vueApp" v-cloak>
        <div class="video-box" data-flex="main:center cross:center">
            <video id="video" preload="meta" autoplay x5-playsinline="" playsinline="" webkit-playsinline="" :src="url" :poster="url+'?x-oss-process=video/snapshot,t_100,m_fast'">
                <source :src="url" type="video/mp4"/>
                <source :src="url" type="video/ogg"/>
                <source :src="url" type="video/webm" />
            </video>
            <img v-if="!status" src="../../image/ui/play.png" alt="">
        </div>
        <div class="footer" data-flex="box:justify cross:center">
            <img :src="!status ? '../../image/ui/play_4.png' : '../../image/ui/pause.png'" alt="">
            <div class="progress-box">
                <div :style="'width:'+ (currentTime/duration*100) +'%'" class="progress"></div>
                <div :style="'left:'+ (currentTime/duration*100) +'%'" class="point"></div>
            </div>
            <p class="time">{{currentTime|videoTime}}</p>
        </div>
    </div>
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/service/service-tool.js"></script>
<script type="text/javascript" src="../../script/service/service-app.js"></script>
<script type="text/javascript" src="../../script/service/service-user.js"></script>
<script type="text/javascript" src="../../script/service/service-logic.js"></script>
<script type="text/javascript" src="../../script/jquery.min.js"></script>
<script type="text/javascript" src="../../script/vue.min.js"></script>
<script type="text/javascript">
    // html 的引号一律为双引号(""),js 为单引号('')
    var USER, $S, vm;
    var video, pageParam = {};
    var isTouch = false;
    apiready = function(){
        $S = SERVICE();
        USER = USER();
        console.log(JSON.stringify(api.pageParam) + '=====pageParam');
        pageParam = api.pageParam;
        fnInit();
    };

    // 初始化
    function fnInit() {
        vm = new Vue({
            el: '.vueApp',
            data: {
                status: false,
                duration: 0,
                currentTime: 0,
                url: pageParam.url
            },
            filters: {
                videoTime: function(time) {
                    time = parseInt(time);
                    var s = time%60,
                        m = (time - s)/60;
                    return (m>9? m : '0'+m)+':'+(s>9? s : '0'+s);
                }
            }
        });
        video = document.getElementById('video');
        video.addEventListener('timeupdate', timeUpdate);
        video.addEventListener("ended", function () {
            vm.status = false;
            vm.currentTime = 0;
        });
        video.addEventListener('play', function() {
            vm.duration = video.duration;
            video.play();
            vm.status = true;
            console.log('canpaly');
        });
        fnAddSliderEvent();
    }

    function timeUpdate() {
        if(!isTouch) {
            var c = video.currentTime;
            if(c - vm.currentTime >= 0.5) {
                vm.currentTime = c;
            }
        }
    }

    // 播放暂停
    function handlePlay() {
        vm.status = !vm.status;
        if(vm.status) {
            video.play();
        }else {
            video.pause();
        }
    }

    function fnAddSliderEvent() {
        var startX;
        var startY;
        var currentX;
        var currentY;
        var progressWidth = document.getElementsByClassName('progress-box')[0].clientWidth;
        var point =  document.getElementsByClassName('point')[0];
        point.addEventListener('touchstart', function(event) {
            startX = event.touches[0].clientX;
            startY = event.touches[0].clientY;
            isTouch = true;
        })

        point.addEventListener('touchmove', function(event) {
            var progressBox = document.getElementsByClassName('progress-box')[0];
            // 防止拖动超出进度条
            if(event.touches[0].clientX >= progressBox.offsetLeft && event.touches[0].clientX <= progressBox.offsetLeft + progressWidth - 6) {
                currentX = event.touches[0].clientX;
            }
            currentY = event.touches[0].clientY;
            var distance = currentX - startX;
            startX = currentX;
            if(distance) {
                var time = vm.duration * (distance / progressWidth);
                vm.currentTime = vm.currentTime + time;
                video.currentTime = vm.currentTime;
            }
        })

        point.addEventListener('touchend', function() {
            isTouch = false;
        })
    }
    
</script>
</html>
